<!--  -->
<template>
  <div class="cart-bottom-bar">
    <div class="check">
      <check-goods-btn :isSelected="isSelectAllGoods" @click.native="handleSelectAllGoods"></check-goods-btn>
      <span>全选</span>
    </div>
    <div class="all-price">合计: ￥{{ totalPrice }}</div>
    <div class="settle-accounts">去结算 ({{ totalNum }})</div>
  </div>
</template>

<script>
import CheckGoodsBtn from "components/content/checkGoodsBtn/CheckGoodsBtn";

import { mapGetters } from "vuex";

export default {
  data() {
    return {};
  },
  components: {
    CheckGoodsBtn,
  },
  methods: {
		// 这里的isSelectAllGoods是点击前的状态
		// 而且这里不能使用下面方法
		//  this.cartList.map((item) => (item.isSelected = !isSelectAllGoods)
		// isSelectAllGoods 和 isSelected在相互影响
    handleSelectAllGoods() {
      if (this.isSelectAllGoods) {
        this.cartList.map((item) => (item.isSelected = false))
      } else {
        this.cartList.map((item) => (item.isSelected = true));
      }
    },
  },
  computed: {
    ...mapGetters(["cartList", "cartListLength"]),
    totalPrice() {
      return this.cartList
        .filter((item) => {
          return item.isSelected;
        })
        .reduce((prev, curr) => {
          return prev * 1 + curr.realPrice * curr.count;
        }, 0)
        .toFixed(2);
    },
    isSelectAllGoods() {
      if (
        this.cartList.length &&
        this.cartList.every((item) => {
          return item.isSelected;
        })
      ) {
        return true;
      }
      return false;
    },
    totalNum() {
      return this.cartList
        .filter((item) => item.isSelected)
        .reduce((prev, curr) => {
          return prev + curr.count;
        }, 0);
    },
  },
};
</script>
<style scoped>
.cart-bottom-bar {
  position: fixed;
  bottom: 49px;
  left: 0;
  right: 0;
  height: 40px;
  background: #f6f6f6;
  display: flex;
  justify-content: space-between;
  align-items: center;
  padding: 0 0 0 8px;
  text-align: center;
}

.cart-bottom-bar .check {
  width: 70px;
  display: flex;
}

.cart-bottom-bar .check span {
  margin-left: 8px;
}

.cart-bottom-bar .all-price {
  flex: 1;
}

.cart-bottom-bar .settle-accounts {
  width: 120px;
  height: 100%;
  line-height: 40px;
  color: #fff;
  background: red;
}
</style>